<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="/WEB-INF/utils.tld" prefix="utils" %> 
<%@ page import="java.util.GregorianCalendar" %>
<%@page import="java.util.ArrayList"%>
<%@page import="fr.unice.CD.calendar.Event"%>
<%@page import="fr.unice.CD.calendar.LoadXML"%>
<%
//on initialise les var de dates
String months[] = {"janv.", "fevr.", "mars", "avr.", "mai", "juin", "juil.", "aout.", "sept.", "oct.", "nov.", "dec.", }; ;
String jours[] = {"Dimanche", "Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi"}; 	
GregorianCalendar aujourdhui= null;
int position, jour, newposition, mois, annee, demainJour, 
demainMois, demainAnnee, hierJour, hierMois, hierAnnee;

//recupere la date passer en URL
String param = request.getParameter("date");
if(param !=null){
	position = param.indexOf("\\");
	jour = Integer.parseInt(param.substring(0,position));
	newposition = param.indexOf("\\",position+1);
	mois = Integer.parseInt(param.substring(position+1, newposition));
	annee = Integer.parseInt(param.substring(newposition+1));

	//initialise un nouveau calendrier en fonction de la date passer en URL
	aujourdhui = new GregorianCalendar(annee, mois-1, jour);
	
	GregorianCalendar demain = (GregorianCalendar)aujourdhui.clone();
	demain.add(GregorianCalendar.DATE, +1);
	demainJour = demain.get(GregorianCalendar.DATE);
	demainMois = demain.get(GregorianCalendar.MONTH)+1;
	demainAnnee = demain.get(GregorianCalendar.YEAR);
	
	GregorianCalendar hier = (GregorianCalendar)aujourdhui.clone();
	hier.add(GregorianCalendar.DATE, -1);
	hierJour = hier.get(GregorianCalendar.DATE);
	hierMois = hier.get(GregorianCalendar.MONTH)+1;
	hierAnnee = hier.get(GregorianCalendar.YEAR);
} 
else{
	aujourdhui = new GregorianCalendar();
	jour = aujourdhui.get(GregorianCalendar.DATE);
	mois = aujourdhui.get(GregorianCalendar.MONTH);
	annee = aujourdhui.get(GregorianCalendar.YEAR);
	
	GregorianCalendar demain = (GregorianCalendar)aujourdhui.clone();
	demain.add(GregorianCalendar.DATE, +1);
	demainJour = demain.get(GregorianCalendar.DATE);
	demainMois = demain.get(GregorianCalendar.MONTH)+1;
	demainAnnee = demain.get(GregorianCalendar.YEAR);
	
	GregorianCalendar hier = (GregorianCalendar)aujourdhui.clone();
	hier.add(GregorianCalendar.DATE, -1);
	hierJour = hier.get(GregorianCalendar.DATE);
	hierMois = hier.get(GregorianCalendar.MONTH)+1;
	hierAnnee = hier.get(GregorianCalendar.YEAR);
}
%>
<html>
	<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>CD-Calendar</title>
	<link href="style.css" rel="stylesheet" type="text/css" />
	<jsp:useBean id="event" class="fr.unice.CD.calendar.BeanEvent"scope="application" />
		<%
		if(event.getLesEvent().size()==0){
			String path = this.getServletContext().getRealPath(
					request.getContextPath());
			LoadXML.loadXML(path, event);
		}
		%>
	<script language="JavaScript" src="script.js"></script>
	</head>
	
	<body>
	<h2>Mon Agenda</h2>
	<div  id="granularite">	
		<ul>
		<li><a href="jour.jsp">Jour</a></li>
		<li><a href="semaine.jsp">Semaine</a></li>
		<li><a href="mois.jsp">Mois</a></li>
		<li><a href="planning.jsp">Mon planning</a></li>
		</ul>
	</div>
	
	<div id="colonnegauche">
		<a href="ajouter.jsp">Créer un evenement</a><br/>
		<div id="divcalendar"><utils:monCal/></div>
	</div>
	
	<div id="colonnedroite">
		<%
		if(param !=null){
			%>
			<a href="planning.jsp?date=<%=hierJour+"\\"+hierMois+"\\"+hierAnnee %>"><input type="button" value=" < " class="buttonImg" onclick="preDay();"></a>
			<a href="planning.jsp?date=<%=demainJour+"\\"+demainMois+"\\"+demainAnnee %>"><input type="button" value=" > " class="buttonImg" onclick="nextDay();"></a>
			<a href="planning.jsp"><input type="button" value="Aujourd'hui" class="buttonImg"></a>
			&nbsp;&nbsp;
			<%
			
		//affiche la date au format 30 avr. 2008
			out.println("<b>"+jour+" "+months[mois-1]+" "+annee+"</b>");
		} 
		else{
			%>
			<a href="planning.jsp?date=<%=hierJour+"\\"+hierMois+"\\"+hierAnnee %>"><input type="button" value=" < " class="buttonImg" onclick="preDay();"></a>
			<a href="planning.jsp?date=<%=demainJour+"\\"+demainMois+"\\"+demainAnnee %>"><input type="button" value=" > " class="buttonImg" onclick="nextDay();"></a>
			<a href="planning.jsp"><input type="button" value="Aujourd'hui" class="buttonImg"></a>
			&nbsp;&nbsp;
			<%
			out.println("<b>"+jour+" "+months[mois]+" "+annee+"</b>");
		}
		%>
		<table id="agendaHead">
			<thead>
					<tr>
						<th border="1"  align="center">
						Tous les événements de mon Agenda
						</th>
					</tr>
			</thead>
			</table>
			<div id="overflowAgenda">
			<div id="evenements"></div>
					<table id="agenda">
					<tbody>	
						<%
						int dayOfWeekparam = -1;
						
						//on affiche seulement tous les evenements
						ArrayList<Event> listeEvents=event.getLesEvent();
						Event evenement;
						for (int i=0;i<listeEvents.size();i++){
							evenement=listeEvents.get(i);
						
							
							int pos = evenement.getDateDebut().indexOf("/");
							int tempjour = Integer.parseInt(evenement.getDateDebut().substring(0,pos));
							int newpos = evenement.getDateDebut().indexOf("/",pos+1);
							int tempmois = Integer.parseInt(evenement.getDateDebut().substring(pos+1, newpos));
							int tempannee = Integer.parseInt(evenement.getDateDebut().substring(newpos+1));
							GregorianCalendar tempCalend = new GregorianCalendar(tempannee, tempmois-1, tempjour);
							dayOfWeekparam = tempCalend.get(GregorianCalendar.DAY_OF_WEEK);
							tempmois= tempmois-1;
							String date = tempjour+"/"+tempmois+"/"+tempannee;
							
							out.println("<tr>");
							out.println("<td><a href=\"javascript: visibiliteModif('"+evenement.getNom()+"', '"+i+"', '"+evenement.getHeureDebut()+"', '"+evenement.getDuree()+"', '"+dayOfWeekparam+"',  '"+date+"');\" >"+ evenement.getNom() +"</a></td>");
							out.println("<td>"+ evenement.getDateDebut() +"</td>");
							out.println("<td>Heure de début : "+ evenement.getHeureDebut() +"</td>");
							out.println("<td>Lieu : "+ evenement.getLieu() +"</td>");
							out.println("<td>"+ evenement.getDescriptif()+"</td>");
							out.println("</tr>");
						} //fin FOR
						%>

					</tbody>
				</table>		
			</div>
			
				
		<div id="divModif" style="display: none;"><b>
			<div id="nameEventSelect">nom Event</div>
			<div id='heureClique'></div>
			<form name="supprForm" id="supprForm" method='get' action='Suppression'>
			<input type='hidden' id="idEvent" name='id' value=''> [<a
				href="javascript: submitform('supprForm')">supprimer</a>]<br />
			</form>
			<hr>
			<form name="modifForm" id="modifForm" method='get' action='modifier.jsp'>
			<input type='hidden' id="idEvent2" name='id' value=''> <a
				href="javascript: submitform('modifForm')"> modifier les details de
			l'evenement >></a></form>
		</div>
		
	</div><!-- //col de droite -->
		
	</body>
</html>